package com.cars.theadpool.controller;

import com.cars.theadpool.service.AsyncService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * @author: caoqianfan
 * @date: 2023/11/13 9:35
 */
@RestController
@RequestMapping("/thread")
public class ThreadController {
    @Resource
    private AsyncService asyncService;
    @Resource
    private ThreadPoolTaskExecutor threadPool;

    @RequestMapping("/test")
    public void testAsync() throws InterruptedException {
        System.out.println("testAsync 执行开始");
        TimeUnit.SECONDS.sleep(2);
        asyncService.executeAsync();
        System.out.println("testAsync 执行完毕");
    }

    @RequestMapping("/testThreadPool")
    public void testThreadPool() throws InterruptedException {
        System.out.println("testThreadPool start");
        TimeUnit.SECONDS.sleep(2);
        threadPool.execute(() -> System.out.println("threadPool testThreadPool"));
        System.out.println("testThreadPool end");
    }
}